package com.example.order.controller;

/**
 * @author FanFu
 * @version 1.0
 * @date 2025/11/8 20:26
 */
import com.example.common.model.Result;
import com.example.order.entity.Order;
import com.example.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{id}")
    public Result<Order> getOrderById(@PathVariable @NotNull Long id) {
        Order order = orderService.getOrderById(id);
        return Result.success(order);
    }

    @GetMapping("/user/{userId}")
    public Result<List<Order>> getOrdersByUserId(@PathVariable @NotNull Long userId) {
        List<Order> orders = orderService.getOrdersByUserId(userId);
        return Result.success(orders);
    }

    @PostMapping
    public Result<Boolean> createOrder(@RequestBody @Valid Order order) {
        boolean result = orderService.save(order);
        return Result.success(result);
    }

    @PutMapping
    public Result<Boolean> updateOrder(@RequestBody @Valid Order order) {
        boolean result = orderService.updateById(order);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteOrder(@PathVariable @NotNull Long id) {
        boolean result = orderService.removeById(id);
        return Result.success(result);
    }

    @GetMapping("/list")
    public Result<List<Order>> getOrderList() {
        List<Order> list = orderService.list();
        return Result.success(list);
    }
}